package com.platform.server.util;

import java.util.HashMap;
import java.util.Map;

import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayObject;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayFundTransToaccountTransferRequest;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.pingplusplus.Pingpp;
import com.pingplusplus.exception.APIConnectionException;
import com.pingplusplus.exception.APIException;
import com.pingplusplus.exception.AuthenticationException;
import com.pingplusplus.exception.ChannelException;
import com.pingplusplus.exception.InvalidRequestException;
import com.pingplusplus.exception.RateLimitException;
import com.pingplusplus.model.Transfer;
import com.platform.server.response.pojo.ReturnData;

import net.sf.json.JSONObject;

public class CfbpayDemoUtils {
	/**
	 * 支付宝支付调用需要参数
	 * 
	 * @param body
	 *            订单描述（自定义）
	 * @param subject
	 *            订单标题（自定义）
	 * @param out_trade_no（订单id）
	 *            商户订单号 orderSn
	 * @param total_amount
	 *            订单总金额，单位为元，精确到小数点后
	 * @param order_id
	 *            回传参数
	 */
	public static ReturnData returnAlipayPayParam(String body, String subject, String out_trade_no, String total_amount,String param) throws Exception {
		ReturnData result = new ReturnData("");
		// 业务参数,
		Map<String, String> m = new HashMap<String, String>();
		m.put("body", body);					//
		m.put("subject", subject);
		m.put("out_trade_no", out_trade_no);
		m.put("timeout_express", "30m"); 
		m.put("total_amount", total_amount);
		m.put("seller_id", AlipayCons.ALIPAY_PARTNER);
		m.put("product_code", "QUICK_MSECURITY_PAY");
		m.put("passback_params", param); // 回传参数，回调时原值返回
		// 业务内容:json数据
		JSONObject bizcontentJson = JSONObject.fromObject(m);
		// *************************************************************************************
		// 实例化客户端
		String gateway = "https://openapi.alipay.com/gateway.do";
		AlipayClient alipayClient = new DefaultAlipayClient(gateway, AlipayCons.ALIPAY_APP_ID,
				AlipayCons.ALIPAY_PRIVATE_KEY, "json", "utf-8", AlipayCons.ALIPAY_PUBLIC_KEY, "RSA2");
		// 实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.open.public.template.message.industry.modify
		AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
		// SDK已经封装掉了公共参数，这里只需要传入业务参数
		// 此次只是参数展示，未进行字符串转义，实际情况下请转义
		System.out.println(alipayClient.toString());
		request.setNotifyUrl(AlipayCons.PAY_NOTIFY_URL);	//回调地址
		request.setBizContent(bizcontentJson.toString());
		AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
		// 调用成功，则处理业务逻辑
		if (response.isSuccess()) {
			result.setData(response.getBody());
		} else {
			result.setCode(Integer.parseInt(response.getCode()));
			result.setMsg(response.getSubMsg());
		}
		return result;// (jsonBuffer.toString());
	}

	// 提现
	public static ReturnData payWithdrawParam(String out_trade_no, String payee_account, String total_amount) throws Exception {
		ReturnData result = new ReturnData("");
		// 业务参数,
		Map<String, String> m = new HashMap<String, String>();
		m.put("out_biz_no", out_trade_no);					
		m.put("payee_type", "ALIPAY_LOGONID");
		m.put("payee_account", payee_account);
		m.put("amount", total_amount); 
		// 业务内容:json数据
		JSONObject bizcontentJson = JSONObject.fromObject(m);
		// *************************************************************************************
		// 实例化客户端
		String gateway = "https://openapi.alipay.com/gateway.do";
		AlipayClient alipayClient = new DefaultAlipayClient(gateway, AlipayCons.ALIPAY_APP_ID,
				AlipayCons.ALIPAY_PRIVATE_KEY, "json", "utf-8", AlipayCons.ALIPAY_PUBLIC_KEY, "RSA2");
		// 实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.open.public.template.message.industry.modify
		AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();	
		// SDK已经封装掉了公共参数，这里只需要传入业务参数
		// 此次只是参数展示，未进行字符串转义，实际情况下请转义
		request.setBizContent(bizcontentJson.toString());
		 AlipayFundTransToaccountTransferResponse response = alipayClient.execute(request);
		// 调用成功，则处理业务逻辑
		if (response.isSuccess()) {
			result.setCode(200);
			result.setMsg(response.getSubMsg());
		} else {
			result.setData(response.getBody());
			result.setCode(Integer.parseInt(response.getCode()));
		}
		return result;// (jsonBuffer.toString());
	}
	
	
	
	//ping++支付
	public static ReturnData transfer(Double amount,String cardNumber,String userName,String openBankCode,Integer type){
		ReturnData result = new ReturnData("");
		Pingpp.apiKey="sk_live_LCyvz9HyvjbTKqTGOG9WHGGO";
		Pingpp.privateKey = PingCons.privateKey;
		Map<String, Object> transfers = new HashMap<String, Object>();
		transfers.put("amount", amount*100);
		transfers.put("currency", "cny");
		transfers.put("order_no",  System.currentTimeMillis());
		transfers.put("description", "用户提现");
		transfers.put("type",  "b2c");
		Map<String, String> app = new HashMap<String, String>();
		app.put("id", "app_rfLSS08e5CCGb1uT");
		Map<String, String> extra = new HashMap<String, String>();
		if(type==2){
			transfers.put("recipient", cardNumber);
			transfers.put("channel", "alipay");
			extra.put("recipient_name", userName);
		}
		if(type==1){
			transfers.put("channel", "chanpay");
			extra.put("user_name", userName);
			extra.put("card_number", cardNumber);
			extra.put("open_bank_code", openBankCode);	
		}
		transfers.put("app", app);
		transfers.put("extra", extra);
		 try {
			Transfer create = Transfer.create(transfers);
			Transfer.retrieve(create.getId());
			System.out.println(create.toString());
			    result.setMsg("提现成功");
				result.setCode(200);
				result.setData(create.getId());
		} catch (Exception e) {
			e.printStackTrace();
			result.setMsg(e.getMessage());
			result.setCode(100);
			return  result;
		}
			return  result;
		
	}
	

	public static void main(String[] args) {
		try {
			ReturnData payWithdrawParam = CfbpayDemoUtils.transfer(1.00,"13265235733","钟志豪",null,2);
	
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
